
var MainScript={

    InitSlideDown:function(){
        $("#divViewComment").click(function(){
            $("#comments").slideToggle('1000');
        })
        $("#divSendComment").click(function(){
            $("#sendComment").slideToggle('1000',function(){
                checkCommentInfo();
            })
        })
    },

    ChangeSource:function(){
        $(".imgItem").click(function(){
            $("#mainImage").attr("src", $(this).attr("src"));
        });
    },

    GoToCart:function(){
        $("img[id='buyNow']").click(function(){
            window.location.href = 'Cart.php?id='+$("#productId").val();
        });
    },

    init:function(){
        MainScript.InitSlideDown();
        MainScript.ChangeSource();
        MainScript.GoToCart();
    }
}

function isEmpty(val) {
    if (val.match(/^s+$/) || val == "")
    {
        return true;
    }
    else
    {
        return false;
    }
}

// kiểm tra email hợp lệ
var emailfilter = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
function isEmailAddress(val) {
    if (val.match(emailfilter))
    {
        return true;
    }
    else
    {
        return false;
    }
}

function checkCommentInfo(){
    var email = $("[name='commentEmail']").val();
    var content = $("[name='commentContent']").val();
    var capcha = $("[name='commentCapcha']").val();
    onCheckContent(content);
    onCheckEmail(email);
    onCheckCapcha(capcha);
    if(onCheckContent(content)&&onCheckCapcha(capcha)&&onCheckEmail(email)){
        $("#alertEmail").text("");
        $("#alertConfirm").text("");
        $("#alertContent").text("");
        $("#tdSubmit").html("<input class=\"btn\" name=\"btnSend\" id=\"btnSend\" type=\"submit\" value=\"Gửi bình luận\" style=\"margin-top:10px; color:#ffffff;\">");

    }else{
        $("#tdSubmit").html("");
    }
}

function onCheckEmail(email){
    if(email!=""){
        if(isEmailAddress(email)){
            $("#alertEmail").text("");
            return true;
        }else{
            $("#alertEmail").text("Email không hợp lệ?");
            return false;
        }
    }
    else{
        $("#alertEmail").text("(?)");
        return false;
    }
}

function onCheckCapcha(capcha){
    if(capcha!=""){
        $("#alertConfirm").text("");
        return true;
    }else{
        $("#alertConfirm").text("(?)");
        return false;
    }
}

function onCheckContent(content){
    if(content!=""){
        $("#alertContent").text("");
        return true;
    }else{
        $("#alertContent").text("(?)");
        return false;
    }
}
